<template>
  <div class="first-step">
    <!-- 表单 -->
    <el-form ref="firstForm" :model="firstForm" :rules="choiceRules" label-width="287px" >
      <el-form-item label="活动名称" prop="name">
        <el-input v-model="firstForm.name" style="width:400px;"/>
      </el-form-item>
      <el-form-item label="活动类型" prop="type">
        <el-radio-group v-model="firstForm.type" @change="pushChangeRadio">
          <el-radio label="limitDiscount">限时打折</el-radio>
          <el-radio label="limitFree">限时免费</el-radio>
          <el-radio label="allDiscount">全站打折</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker v-model="firstForm.startTime" type="datetime" placeholder="选择时间" style="width:400px;"/>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker v-model="firstForm.endTime" type="datetime" placeholder="选择时间" style="width:400px;"/>
        <br><span v-if="message" style="color: red">&nbsp;&nbsp;{{ message }}</span>
      </el-form-item>
      <el-form-item v-if="firstForm.type === 'allDiscount'" label="打折力度" prop="rate" >
        <el-input v-model="firstForm.rate" style="width:200px;">
          <template slot="append">折</template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="nextStep">下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Firststep',
  props: {
    firstForm: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      message: '',
      rules: {
        name: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      },
      allRules: {
        name: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        rate: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    choiceRules() {
      if (this.firstForm.type === 'allDiscount') {
        return this.allRules
      } else {
        return this.rules
      }
    }
  },
  methods: {
    // 单选框改变Step值
    pushChangeRadio() {
      this.$emit('push', this.firstForm.type)
    },
    nextStep() {
      this.$refs.firstForm.validate((valid) => {
        if (valid) {
          if (this.firstForm.startTime >= this.firstForm.endTime) {
            this.message = '结束时间不能早于或等于开始时间'
          } else {
            this.message = ''
            this.firstForm.target = '课程'
            this.$emit('next', this.firstForm)
          }
        }
      })
    }
  }
}
</script>

<style lang="scss">
  .first-step{
    margin-top:40px;
    .el-form{
      .el-form-item{
        .el-form-item__content{
          box-sizing: border-box;
          padding-left:30px;
        }
      }
    }
  }
</style>
